#pragma once
#include<vector>
using namespace std;
namespace zyq
{
	template<class T,class Container=vector<T>,class Cmp=Less<T>>
	class priority_queue
	{
	public:
		void adjustUp(size_t child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				 if (_cmp(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (parent - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjustDown(size_t parent)
		{
			size_t child = parent * 2 + 1;
			while (child<_con.size())
			{
				if (child+1 < _con.size( )&& _cmp(_con[child], _con[child + 1]))
				{
					child++;
				}

				if (_cmp(_con[parent],_con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = child * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjustUp(_con.size()-1);
		}
		
		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjustDown(0);
		}
		
		bool empty()
		{
			return _con.empty();
		}

		const size_t size() const
		{
			return _con.size();
		}

		const T& top() const
		{
			return _con[0];
		}
	private:
		Container _con;
		Cmp _cmp;
	};

	template<class T>
	struct Less
	{
		bool operator()(const T& a, const T& b)
		{
			return a < b;
		}
	};

	template<class T>
	struct Greater
	{
		bool operator()(const T& a, const T& b)
		{
			return a > b;
		}
	};
}

	
